package com.wuyou.array;

import java.util.HashMap;
import java.util.Map;

/**
 * 删除排序数组中的重复项
 */
public class RemoveDuplicates {


    public static void main(String[] args) {
        RemoveDuplicates removeDuplicates = new RemoveDuplicates();
        int[] nums = {0,0,1,1,1,2,2,3,3,4};
        System.out.println(removeDuplicates.solution_2(nums));

    }

    public int solution_2(int[] nums) {
        int index = 0;

        for (int i = 0; i < nums.length - 1; i++) {
            nums[index] = nums[i];
            if (nums[i] == nums[i + 1]) {
                nums[i + 1] = 0;
                continue;
            }
            nums[++index] = nums[i];
        }
        return index;
    }

    public int solution_1(int[] nums) {

        int right = nums.length - 1;
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i <= right; i++) {
            int count = map.getOrDefault(nums[i], 0) + 1;
            if (count > 1) {
                int temp = nums[i];
                System.arraycopy(nums, i + 1, nums, i, nums.length - (i + 1));
                nums[right] = 0;
                right--;
                i--;
            }
            map.put(nums[i], count);
        }
        return right + 1;
    }
}
